package com.lswms.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Money工具类
 * 
 *
 */
public final class MoneyUtil {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MoneyUtil.class);

    private static final int TWO = 2;
    /**
     * 私有构造方法，禁止实例化
     */
    private MoneyUtil() {
    }

    /**
     * 把BigDecimal转为带精度的字符串，直接截取，不四舍五入(改为使用DecimalFormat)
     * 
     * @param money
     * @param scale
     *            小数点后位数。小于或等于0，等于0表示没有小数
     * @return formatStr
     */
    public static String bigDecimal2String(BigDecimal money, int scale) {
        if (money == null) {
            money = new BigDecimal(0);
        }
        StringBuilder patternBd = new StringBuilder("0");
        if (scale > 0) {
            patternBd.append(".");
            for (int i = 0; i < scale; i++) {
                patternBd.append("0");
            }
        }
        return new DecimalFormat(patternBd.toString()).format(money);
    }

    /**
     * 四舍五入
     * @param money
     * @param scale
     * @return
     */
    public static String bigDecimal2StringRound(BigDecimal money, int scale) {
        if (money == null) {
            money = new BigDecimal(0);
        }
        money = money.setScale(2,BigDecimal.ROUND_HALF_UP);
        StringBuilder patternBd = new StringBuilder("0");
        if (scale > 0) {
            patternBd.append(".");
            for (int i = 0; i < scale; i++) {
                patternBd.append("0");
            }
        }
        return new DecimalFormat(patternBd.toString()).format(money);
    }

    /**
     * 两数相加
     * 
     * @param one
     * @param other
     * @return sum
     */
    public static BigDecimal add(BigDecimal one, BigDecimal other) {
        if (one == null) {
            one = new BigDecimal(0.00);
        }
        if (other == null) {
            other = new BigDecimal(0.00);
        }
        return one.add(other);
    }

    public static BigDecimal subtract(BigDecimal one, BigDecimal other) {
        if (one == null) {
            one = new BigDecimal(0.00);
        }
        if (other == null) {
            other = new BigDecimal(0.00);
        }
        return one.subtract(other);
    }
    
    public static BigDecimal multiply(BigDecimal one, BigDecimal other) {
        if (one == null) {
            one = new BigDecimal(0.00);
        }
        if (other == null) {
            other = new BigDecimal(0.00);
        }
        return one.multiply(other);
    }

    /**
     * 字符串转BigDecimal
     * 
     * @param money
     * @return BigDecimal
     */
    public static BigDecimal convertBigDecimal(String money) {
        try {
            if(StringUtils.isEmpty(money)){
                return new BigDecimal(0);    
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return new BigDecimal(money);
    }
    
    public static Boolean isMoney(String str) {
        try {
            new BigDecimal(str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    
    public static Boolean IsInt(String str) {
        if(str.indexOf(".00") != -1) {
            str = str.replace(".00", "");
        }else if(str.indexOf(".0") != -1){
            str = str.replace(".0", "");
        }
        
        try {
            Integer.valueOf(str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    

    /**
     * 把BigDecimal转为带精度的字符串，直接截取，不四舍五入(改为使用DecimalFormat)
     * 小数点后两位数
     * @param money
     * @return formatStr
     */
    public static String bigDecimal2String(BigDecimal money) {
        if (money == null) {
            money = new BigDecimal(0);
        }
        StringBuilder patternBd = new StringBuilder("0");

        patternBd.append(".");
        for (int i = 0; i < TWO; i++) {
            patternBd.append("0");
        }

        return new DecimalFormat(patternBd.toString()).format(money);
    }
    
    public static String bfb(BigDecimal a,BigDecimal b){
    	if(a.compareTo(BigDecimal.ZERO) <= 0){
    		return "0%";
    	}
    	if(b.compareTo(BigDecimal.ZERO) <= 0){
    		return "0%";
    	}
    	BigDecimal c = a.divide(b, 10, BigDecimal.ROUND_HALF_UP);
    	BigDecimal d = c.multiply(new BigDecimal(100));

    	if(d.compareTo(new BigDecimal(0.01)) < 0){
    		return bigDecimal2String(d, 3)+"%";
    	}
    	
    	return bigDecimal2String(d)+"%";
    }
    
    
    public static void main(String[] args) {
        System.out.println(IsInt("10.00"));
    }
}
